﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using NCKH.models;
namespace NCKH.Test
{
    public class XMLInputFile
    {
        public static string CreateXMLInputFile(out string fileName, string folderName = null)
        {
            if (folderName == null) folderName = @"data\input";

            fileName = string.Format("vimaru{0}", DateTime.Now.ToString("yyyyMMddHHmmss"));

            string path = String.Format(@"{0}\{1}", System.IO.Directory.GetCurrentDirectory(), folderName);

            if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path);

            path += String.Format(@"\{0}.fet", fileName);

            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }

            XmlTextWriter writer = new XmlTextWriter(path, System.Text.Encoding.UTF8);
            writer.WriteStartDocument(true);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 2;
            writer.WriteStartElement("fet");
            writer.WriteAttributeString("version", "5.19.0");
            {
                writer.WriteStartElement("Institution_Name", "Đại học Hàng Hải Việt Nam");
                writer.WriteEndElement();

                string comment = string.Format("FILENAME = {0} --- DATETIME = {1} --- COPYRIGHT = © 2013 VIMARU, all rights reserved. ", fileName, DateTime.Now);
                writer.WriteStartElement("Comments", comment);
                writer.WriteEndElement();
            }
            writer.WriteEndDocument();
            writer.Close();


            return path;
        }
        public static bool WriteInputValueBase(string pathFile)
        {
            if (!System.IO.File.Exists(pathFile)) return false;

            //-----------------------------------------------------------------------------------------
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(pathFile);
            XmlNode root = xmlDoc.SelectSingleNode("descendant::fet");
            //---------------------Hours-------------------------------------------------------
            root.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "Hours_List", null) as XmlNode);
            xmlDoc.Save(pathFile);
            Methods.WriteHoursList(pathFile, xmlDoc);
            //---------------------Days---------------------------------------------------------------
            root.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "Days_List", null) as XmlNode);
            xmlDoc.Save(pathFile);
            Methods.WriteDaysList(pathFile, xmlDoc);
            //---------------------Student-------------------------------------------------------------
            root.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "Students_List", null) as XmlNode);
            xmlDoc.Save(pathFile);
            Methods.WriteStudentsList(pathFile, xmlDoc);
            //---------------------Teachers-----------------------------------------------------------
            root.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "Teachers_List", null) as XmlNode);
            xmlDoc.Save(pathFile);
           // Methods.WriteTeachersList(pathFile, xmlDoc);
            //---------------------Subjects------------------------------------------------------------
            root.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "Subjects_List", null) as XmlNode);
            xmlDoc.Save(pathFile);
            Methods.WriteSubjectList(pathFile, xmlDoc);
            //---------------------ActivityTags----------------------------------------------------------
            root.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "Activity_Tags_List", null) as XmlNode);
            xmlDoc.Save(pathFile);
            Methods.WriteActivityTagsList(pathFile, xmlDoc);
            //---------------------Buildings--------------------------------------------------------
            root.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "Buildings_List", null) as XmlNode);
            xmlDoc.Save(pathFile);
            Methods.WriteBuildingsList(pathFile, xmlDoc);
            //---------------------Rooms-------------------------------------------------------
            root.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "Rooms_List", null) as XmlNode);
            xmlDoc.Save(pathFile);
            Methods.WriteRoomsList(pathFile, xmlDoc);
            //---------------------Rooms-------------------------------------------------------
            root.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "Activities_List", null) as XmlNode);
            xmlDoc.Save(pathFile);
            Methods.WriteActivitiesList(pathFile, xmlDoc);
            //---------------------Rooms-------------------------------------------------------
            root.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "Space_Constraints_List", null) as XmlNode);
            xmlDoc.Save(pathFile);
            //---------------------Rooms-------------------------------------------------------
            root.AppendChild(xmlDoc.CreateNode(XmlNodeType.Element, "Time_Constraints_List", null) as XmlNode);
            xmlDoc.Save(pathFile);
            return true;
        }
        public static void WriteInputSpaceConstraints(string pathFile)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(pathFile);
            XmlNode root = xmlDoc.SelectSingleNode("descendant::fet/Space_Constraints_List");
            //-------------------------------------------------------------------------------------
            SpaceConstraints.ConstraintBasicCompulsorySpace(xmlDoc, root, pathFile);
        }
        public static void WriteInputTimeConstraints(string pathFile)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(pathFile);
            XmlNode root = xmlDoc.SelectSingleNode("descendant::fet/Time_Constraints_List");
            //-------------------------------------------------------------------------------------
            TimeConstraints.ConstraintBasicCompulsoryTime(xmlDoc, root, pathFile);

            TimeConstraints.ConstraintBreakTimes(xmlDoc, root, pathFile);
        }
        public static void WriteInputExtendSpaceConstraints(List<V_PHONGHOC> phongHocs, string pathFile, int type = 0)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(pathFile);
            XmlNode root = xmlDoc.SelectSingleNode("descendant::fet/Space_Constraints_List");

            ///* Thông tin bổ trợ của tiết giảng đòi hỏi vài phòng học cụ thể */
            SpaceConstraints.ConstraintActivityTagPreferredRooms(xmlDoc, root, pathFile, phongHocs);

            /* Tiết giảng yêu cầu một hay nhiều phòng học cố định */
           // SpaceConstraints.ConstraintActivityPreferredRooms(xmlDoc, root, pathFile, phongHocs.Select(s => s.Phong_hoc).ToList(), suKiens);

            /* Lớp hành chính có một hay nhiều phòng học cố định */
            //SpaceConstraints.ConstraintStudentsSetHomeRooms(xmlDoc, root, pathFile, phongHocs.Select(s => s.Phong_hoc).ToList(), suKiens);

            if (type != 0)
            {
                /* Khóa phòng học đã xếp phòng những thời điểm phòng học bận - Thời gian bận của phòng học */
            }
        }
    }
}
